//
//  WPTradeOrderViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/5.
//  Copyright © 2016年 HuaYiSoftware. All rights reserved.
//

#import "WPTradeOrderViewController.h"

@interface WPTradeOrderViewController ()
@property (nonatomic, strong) UIScrollView * backScrollView;/**< 最下层滚动视图 */
@property (nonatomic, strong) UIView * whiteBackView;/**< 白色背景图 */
@property (nonatomic, strong) UIImageView * introImageView;
@property (nonatomic, strong) UILabel * titleLabel;/**< 标题文本 */
@property (nonatomic, strong) UILabel * nameLabel;/**< 发起人 */
@property (nonatomic, strong) UITextField * payTimeTextField;/**< 倒计时 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 倒计时时间 */

@property (nonatomic, strong) UIView * payDetailView;/**< 支付详情背景图 */
@property (nonatomic, strong) UILabel * payMoneyIntroLabel;/**< 支付金额说明 */
@property (nonatomic, strong) UILabel * payMoneyCountLabel;/**< 支付金额 */
@property (nonatomic, strong) UITextField * receiveTextField;/**< 收款方 */

@property (nonatomic, strong) UIView * tradeRightView;/**< 交易权益 */
@property (nonatomic, strong) UILabel * tradeTitleLabel;/**< 交易标题 */
@property (nonatomic, strong) UILabel * timeLabel;/**< 演出时间 */
@property (nonatomic, strong) UILabel * addressLabel;/**< 演出地点 */
@property (nonatomic, strong) UILabel * positionLabel;/**< 票对应位置 */

@property (nonatomic, strong) UIView * riskView;/**< 风险提示背景图 */
@property (nonatomic, strong) UITextView * riskTextView;/**< 风险提示文本 */
@property (nonatomic, strong) UIButton * spreadBtn;/**< 展开收起按钮 */
@property (nonatomic, strong) UIButton * payBtn;/**< 支付按钮 */

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */


@end

@implementation WPTradeOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
- (void)viewWillDisappear:(BOOL)animated {
    if (_timer.isProxy) {
        
    }
}
#pragma mark - init
- (void)initializeDataSource {
    self.timeCount = 60*10;
}
- (void)initializeUserInterface {
    [self.view addSubview:self.backScrollView];
    [_backScrollView addSubview:self.whiteBackView];
    [_whiteBackView addSubview:self.introImageView];
    [_whiteBackView addSubview:self.titleLabel];
    [_whiteBackView addSubview:self.nameLabel];
    
    [_whiteBackView addSubview:self.payTimeTextField];
    
    [_whiteBackView addSubview:self.payDetailView];
    [self configGrayViewWithPositionY:CGRectGetMaxY(_payDetailView.frame)];
    [_whiteBackView addSubview:self.tradeRightView];
    [self configGrayViewWithPositionY:CGRectGetMaxY(_tradeRightView.frame)];
    [_whiteBackView addSubview:self.riskView];
    [_backScrollView addSubview:self.payBtn];
    
    self.timer.fireDate = [NSDate date];
}
#pragma mark - responds events
- (void)respondsToTimer {
    _timeCount--;
    NSInteger currentSeconds = _timeCount % 60;
    NSInteger currentMinutes = (_timeCount / 60) %60;
    NSString *stingFormat = @"支付剩余时间：%d分钟%d秒" ;
    self.payTimeTextField.text = [NSString stringWithFormat:stingFormat, currentMinutes, currentSeconds];
}
- (void)respondsToSpreadBtn:(UIButton *)sender {
    
}
- (void)respondsToPayBtn {
    
}
#pragma mark - private methods
#pragma mark - 文本自适应
- (CGFloat)culculateHeightWithText:(NSString *)text font:(UIFont *)font {
    CGRect rect = [text boundingRectWithSize:DHFlexibleSize(CGSizeMake(180, MAXFLOAT), NO)  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : font} context:nil];
    return rect.size.height;
}
- (void)configGrayViewWithPositionY:(CGFloat)y {
    UIView * grayView = [[UIView alloc]initWithFrame:CGRectMake(0, y, 310*DHFlexibleHorizontalMutiplier(), 12*DHFlexibleHorizontalMutiplier())];
    grayView.backgroundColor = COLOR_RGB(241, 241, 241, 1);
    [_whiteBackView addSubview:grayView];
}
- (NSMutableAttributedString *)configAttributtedStringWithString:(NSString *)str {
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@元", str]];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(242, 106, 89, 1) range:NSMakeRange(0, str.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(141, 141, 141, 1) range:NSMakeRange(str.length, 1)];
    return attributedStr;
}
#pragma mark - getter
- (UIScrollView *)backScrollView {
    if (!_backScrollView) {
        _backScrollView = ({
            UIScrollView * view = [[UIScrollView alloc]initWithFrame:CGRectMake(5*DHFlexibleHorizontalMutiplier(), navBar_Fheight+10*DHFlexibleHorizontalMutiplier(), 310*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.height - navBar_Fheight-10*DHFlexibleHorizontalMutiplier())];
            view.contentSize = CGSizeMake(0, 1000);
            view;
        });
    }
    return _backScrollView;
}
- (UIView *)whiteBackView {
    if (!_whiteBackView) {
        _whiteBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 310*DHFlexibleHorizontalMutiplier(), 880*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            view.layer.cornerRadius = 5;
            view.layer.masksToBounds = YES;
            view;
        });
    }
    return _whiteBackView;
}
- (UIImageView *)introImageView {
    if (!_introImageView) {
        _introImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 155*DHFlexibleHorizontalMutiplier())];
            view.image = IMAGE_NAME(@"banner1");
            view;
        });
    }
    return _introImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(5*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.introImageView.frame), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(111, 111, 111, 1);
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"发发发发发送方发发发送范德萨范德萨发法师法发";
            lab;
        });
    }
    return _titleLabel;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(5*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.titleLabel.frame), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(211, 211, 211, 1);
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"发发发发发送方发发发送范德萨范德萨发法师法发";
            lab;
        });
    }
    return _nameLabel;
}
- (UITextField *)payTimeTextField {
    if (!_payTimeTextField) {
        _payTimeTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.nameLabel.frame), 310*DHFlexibleHorizontalMutiplier(), 32*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(63*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 12*DHFlexibleHorizontalMutiplier(), 12*DHFlexibleHorizontalMutiplier())];
            imageView.image = IMAGE_NAME(@"djs");
            [leftView addSubview:imageView];
            text.leftView = leftView;
            text.backgroundColor = COLOR_RGB(241, 241, 241, 1);
            text.textColor = COLOR_RGB(234, 27, 47, 1);
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.userInteractionEnabled = NO;
            text;
        });
    }
    return _payTimeTextField;
}
- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (UIView *)payDetailView {
    if (!_payDetailView) {
        _payDetailView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.payTimeTextField.frame), 310*DHFlexibleHorizontalMutiplier(), 140*DHFlexibleHorizontalMutiplier())];
            UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 300, 30*DHFlexibleHorizontalMutiplier())];
            title.textColor = COLOR_RGB(211, 211, 211, 1);
            title.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            title.text = @"支付详情";
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), 310*DHFlexibleHorizontalMutiplier(), 1.5)];
            line.backgroundColor = COLOR_RGB(211, 211, 211, 1);
            [view addSubview:line];
            [view addSubview:title];
            [view addSubview:self.payMoneyIntroLabel];
            [view addSubview:self.payMoneyCountLabel];
            [view addSubview:self.receiveTextField];
            view;
        });
    }
    return _payDetailView;
}
- (UILabel *)payMoneyIntroLabel {
    if (!_payMoneyIntroLabel) {
        _payMoneyIntroLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 40*DHFlexibleHorizontalMutiplier(), 310*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.textColor = COLOR_RGB(211, 211, 211, 1);
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"付款金额";
            lab;
        });
    }
    return _payMoneyIntroLabel;
}
- (UILabel *)payMoneyCountLabel {
    if (!_payMoneyCountLabel) {
        _payMoneyCountLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.payMoneyIntroLabel.frame), 310*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.textColor = COLOR_RGB(211, 211, 211, 1);
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab;
        });
    }
    return _payMoneyCountLabel;
}
- (UITextField *)receiveTextField {
    if (!_receiveTextField) {
        _receiveTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 110*DHFlexibleHorizontalMutiplier(), 290*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(111, 111, 111, 1);
            lab.font = [UIFont boldSystemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.text = @"收款方";
            text.leftView = lab;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(211, 211, 211, 1);
            text.textAlignment = NSTextAlignmentRight;
            text.userInteractionEnabled = NO;
            text.text = @"成都文化产权交易所-众筹";
            text;
        });
    }
    return _receiveTextField;
}
- (UIView *)tradeRightView {
    if (!_tradeRightView) {
        _tradeRightView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.payDetailView.frame)+12*DHFlexibleHorizontalMutiplier(), 310*DHFlexibleHorizontalMutiplier(), 180*DHFlexibleHorizontalMutiplier())];
            UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 300, 30*DHFlexibleHorizontalMutiplier())];
            title.textColor = COLOR_RGB(211, 211, 211, 1);
            title.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            title.text = @"交易权益";
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), 310*DHFlexibleHorizontalMutiplier(), 1.5)];
            line.backgroundColor = COLOR_RGB(211, 211, 211, 1);
            [view addSubview:line];
            [view addSubview:title];
            [view addSubview:self.tradeTitleLabel];
            [view addSubview:self.timeLabel];
            [view addSubview:self.addressLabel];
            [view addSubview:self.positionLabel];
            view;
        });
    }
    return _tradeRightView;
}
- (UILabel *)tradeTitleLabel {
    if (!_tradeTitleLabel) {
        _tradeTitleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier(), 290*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            lab.numberOfLines = 2;
            lab.text = @"321321321312321faf发发发安抚发案发生大发发安抚发安抚是否发发affect发发 大幅度发发呆时";
            lab;
        });
    }
    return _tradeTitleLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.tradeTitleLabel.frame), 300, 20*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            lab.text = @"4月23日 星期六20：00 ~ 22：00";
            lab;
        });
    }
    return _timeLabel;
}
- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.timeLabel.frame), 300, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            lab.text = @"成都体育馆";
            lab;

        });
    }
    return _addressLabel;
}
- (UILabel *)positionLabel {
    if (!_positionLabel) {
        _positionLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.addressLabel.frame), 300, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            lab.text = @"A区1排25号";
            lab;

        });
    }
    return _positionLabel;
}
- (UIView *)riskView {
    if (!_riskView) {
        _riskView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tradeRightView.frame)+12*DHFlexibleHorizontalMutiplier(), 310*DHFlexibleHorizontalMutiplier(), 270*DHFlexibleHorizontalMutiplier())];
            UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 300, 30*DHFlexibleHorizontalMutiplier())];
            title.textColor = COLOR_RGB(211, 211, 211, 1);
            title.font = [UIFont boldSystemFontOfSize:15*DHFlexibleHorizontalMutiplier()];
            title.text = @"风险提示";
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame), 310*DHFlexibleHorizontalMutiplier(), 1.5)];
            line.backgroundColor = COLOR_RGB(211, 211, 211, 1);
            [view addSubview:line];
            [view addSubview:title];
            [view addSubview:self.riskTextView];
            [view addSubview:self.spreadBtn];
            view;
        });
    }
    return _riskView;
}
- (UITextView *)riskTextView {
    if (!_riskTextView) {
        _riskTextView = ({
            UITextView * text = [[UITextView alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier(), 290*DHFlexibleHorizontalMutiplier(), 235*DHFlexibleHorizontalMutiplier())];
            text.font = [UIFont boldSystemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(111, 111, 111, 111);
            text.editable = NO;
            text;
        });
    }
    return _riskTextView;
}
- (UIButton *)spreadBtn {
    if (!_spreadBtn) {
        _spreadBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(230*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.riskTextView.frame)- 30*DHFlexibleHorizontalMutiplier(), 55*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setTitle:@"展开V" forState:UIControlStateNormal];
            [btn setTitle:@"收起^" forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(respondsToSpreadBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn.backgroundColor = COLOR_RGB(241, 241, 241, 241);
            btn;
        });
    }
    return _spreadBtn;
}
- (UIButton *)payBtn {
    if (!_payBtn) {
        _payBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.whiteBackView.frame)+30*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:15*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"立即 支 付" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToPayBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _payBtn;
}
@end
